// api应用的控制器
package apiv1

import (
	"go-mp-starter/app/common/request"
	"go-mp-starter/app/common/response"
	"go-mp-starter/app/services"

	"github.com/gin-gonic/gin"
)

// Register 用户注册控制器
func Register(ctx *gin.Context) {
	var form request.Register
	// 将request中的JSON绑定到form(注意用指针)
	if err := ctx.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(ctx, request.GetErrorMsg(form, err))
		return
	}

	// 执行注册程序
	if user, err := services.UserService.Register(form); err != nil {
		response.BusinessFail(ctx, err.Error())
	} else {
		response.Success(ctx, user)
	}
}

// Info 用户信息
func Info(ctx *gin.Context) {
	user, err := services.UserService.GetUserInfo(ctx.Keys["id"].(string))
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, user)
}
